package com.zhouchao.dubbo.service;

import com.zhouchao.dubbo.ConsumerApplication;
import com.zhouchao.dubbo.HelloService;
import com.zhouchao.dubbo.filter.TPMonitorFilter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.extension.Adaptive;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: zhouchao
 * @Date: 2021/09/18 19:52
 * @Description:
 */
@Slf4j
@Service
public class TestServiceImpl {

    @DubboReference
    private HelloService helloService;

    ExecutorService methodsAService = Executors.newFixedThreadPool(2000);
    ExecutorService methodsBService = Executors.newFixedThreadPool(2000);
    ExecutorService methodsCService = Executors.newFixedThreadPool(2000);

    public String test() {
        TPMonitorFilter tpMonitorFilter = ConsumerApplication.monitorTime;

        for (int i = 0; i < 2000; i++) {
            methodsAService.submit(() -> {
                Long start = System.currentTimeMillis();
                String s = helloService.methodA();
                Long end = System.currentTimeMillis();
                Long time = end - start;
                tpMonitorFilter.serviceReport("com.zhouchao.dubbo.HelloService.methodA", time);
                log.info("{}-{}", s, time);
            });

            methodsBService.submit(() -> {
                Long start = System.currentTimeMillis();
                String s = helloService.methodB();
                Long end = System.currentTimeMillis();
                Long time = end - start;
                tpMonitorFilter.serviceReport("com.zhouchao.dubbo.HelloService.methodB", time);
                log.info("{}-{}", s, time);
            });

            methodsCService.submit(() -> {
                Long start = System.currentTimeMillis();
                String s = helloService.methodC();
                Long end = System.currentTimeMillis();
                Long time = end - start;
                tpMonitorFilter.serviceReport("com.zhouchao.dubbo.HelloService.methodC", time);
                log.info("{}-{}", s, time);
            });
        }
        return "success";
    }

}
